package reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * 反射操作泛型
 */
public class Demo04 {
    public static void main(String[] args) {
        String path="reflectionbean.User";
        try {
            Class clazz = Class.forName(path);
            Method m1=clazz.getDeclaredMethod("test01", Map.class, List.class);
            Type[] t1=m1.getGenericParameterTypes();//获得带泛型参数类型
            for (Type parameType:t1) {
                System.out.println("#"+parameType);
                if (parameType instanceof ParameterizedType){//判断是否是参数类型
                    //强制转为带泛型的参数类型,然后获得真正的参数类型
                    Type[] genericTypes=((ParameterizedType)parameType).getActualTypeArguments();
                    for (Type genericType:genericTypes) {
                        System.out.println("泛型类型:"+genericType);
                    }
                }
            }

            Method m2=clazz.getDeclaredMethod("test02");
            Type returnType=m2.getGenericReturnType();
            System.out.println("#"+returnType);
            if (returnType instanceof ParameterizedType){
                Type[] genericTypes=((ParameterizedType)returnType).getActualTypeArguments();
                for (Type genericType:genericTypes) {
                    System.out.println("返回值,泛型类型:"+genericType);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
